﻿using System;
using System.Collections.Generic;
using System.Text;
using NAnt.Core;
using NAnt.Core.Attributes;
using EnvRide.Core.ObjectModel;
using System.Threading;
using log4net;
using CodeMadeSimple.PropertiesManagement.NAnt.Tasks;

namespace EnvRide.NAnt
{
    [TaskName("envride")]
    public class EnvRideTask : EnvRideBaseTask
    {
        #region Fields

        private PropertyContainerType _propContainerType = PropertyContainerType.Context;
        private EnvRideProcessArguments _arguments;

        #endregion

        #region Properties

        [StringValidator(AllowEmpty = false)]
        [TaskAttribute("environment", Required = true)]
        public String Environment { get; set; }

        [TaskAttribute("defaultenvironment")]
        public String DefaultEnvironment { get; set; }

        [TaskAttribute("outputfilepath")]
        public String OutputPath { get; set; }

        [TaskAttribute("propcontainertype")]
        public PropertyContainerType PropContainerType
        {
            get { return _propContainerType; }
            set { _propContainerType = value; }
        }

        [TaskAttribute("propcontainer")]
        public String PropContainer { get; set; }

        [TaskAttribute("savewithchangesonly")]
        public bool SaveWithChangesOnly { get; set; }

        [TaskAttribute("createoutputfiledir")]
        public bool CreateOutputFileDirectory { get; set; }

        /// <summary>
        /// Last execution process arguments.
        /// </summary>
        public EnvRideProcessArguments Arguments
        {
            get { return _arguments; }
        }

        #endregion

        #region Methods

        protected override void ExecuteTask()
        {
            base.ExecuteTask();

            EnvRideProject proj = EnvRideProject.Deserialize(FilePath);

            if (!String.IsNullOrEmpty(DefaultEnvironment))
                proj.DefaultEnvironmentName = DefaultEnvironment;

            _arguments = new EnvRideProcessArguments();
            _arguments.EnvironmentName = Environment;
            _arguments.OutputFilePath = OutputPath;
            _arguments.SaveOutputFileOnlyIfItHasChanges = SaveWithChangesOnly;
            _arguments.CreateOutputFileDirectory = CreateOutputFileDirectory;
            
            if (!String.IsNullOrEmpty(PropContainer))
            {
                switch (PropContainerType)
                {
                    case PropertyContainerType.Context:
                        _arguments.PropertyContainer = NAntPropertyContextManager.Instance.GetContext(PropContainer);
                        break;

                    case PropertyContainerType.Stack:
                        _arguments.PropertyContainer = NAntPropertyContextManager.Instance.GetStack(PropContainer);
                        break;

                    default:
                        throw new NotSupportedException(String.Format("The PropertyContainerType \"{0}\" its not supported!", PropContainerType));
                }
            }

            proj.Process(_arguments);
        }

        #endregion
    }
}
